<template>
  <div class="component-wrapper" :style="wrapper_style" @click="toUrl">
    <div v-if="datas.show_title" :style="title_style" class="title">{{ title }}</div>
    <div v-if="datas.show_second_title" :style="second_title_style" class="second-title">{{ second_title }}</div>
    <div style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" v-if="datas.link.is_h5 == 2 && !fun.isApp() && !fun.isCPS()" @click.stop v-html="wxhtml"></div>
  </div>
</template>

<script>
export default {
  props: ['id', 'datas'],
  data() {
    return {
      wxhtml: ''
    };
  },
  created() {
    this.$nextTick(() => {
      this.initWxhtml();
    });
  },
  activated() {
    this.$nextTick(() => {
      this.initWxhtml();
    });
  },
  computed: {
    /** 样式 */
    wrapper_style() {
      const {
        preview_color,
        padding_top,
        padding_bottom,
        padding_left,
        padding_right,
      } = this.datas;

      return `
                width: 100%;
                padding-top: ${padding_top}px;
                padding-bottom: ${padding_bottom}px;
                padding-left: ${padding_left}px;
                padding-right: ${padding_right}px;
                background-color: ${preview_color.color};
                background-image: url(${preview_color.isColor == 2? preview_color.image: ''});
                background-size: 100% auto;
                bakcground-position: center;
            `;
    },

    /** 标题样式 */
    title_style() {
      const {
        text_style,
        text_align,
        text_size,
        text_color
      } = this.datas;
      return `
                font-weight: ${text_style.includes('1')?'bold':''};
                font-style: ${text_style.includes('2')?'italic':''};
                text-align: ${text_align};
                font-size: ${text_size}px;
                color: ${text_color};
            `;
    },

    /** 副标题样式 */
    second_title_style() {
      const {
        second_text_style,
        second_text_align,
        second_text_size,
        second_text_color
      } = this.datas;
      return `
                font-weight: ${second_text_style.includes('1')?'bold':''};
                font-style: ${second_text_style.includes('2')?'italic':''};
                text-align: ${second_text_align};
                font-size: ${second_text_size}px;
                color: ${second_text_color};
            `;
    },

    /** 标题 */
    title() {
      return this.datas.title || '标题';
    },
    second_title() {
      return this.datas.second_title || '副标题';
    },

  },
  methods: {
    toUrl() {
      let item = this.datas.link;
      if(item.is_h5 == 2 && this.fun.isApp()) {
        YDB.MiniWX(item.gh_id, item.minApp_link);
        return;
      }
      if(item.is_h5 == 2 &&  this.fun.isCPS()) {
        let json = {
          mini_route: item.minApp_link,
          mini_id: item.gh_id,
        };
        if (this.fun.isIosOrAndroid() === "ios") {
          miniWXIOS({ body: "miniWXIOS", json: JSON.stringify(json) });
        } else {
          miniWXAndroid(JSON.stringify(json));
        }
        return;
      }

      if(item.is_h5 == 7) {
        if (this.fun.isCPS() && this.fun.isCPSAPPI()) {
          let json = { name: 'task' };
          if (this.fun.isIosOrAndroid() === "ios") {
            toUrlIOS({ body: "toUrlIOS", json: JSON.stringify(json) });
          } else {
            toUrlAndroid(JSON.stringify(json));
          }
        }else if(item.link) {
          window.location.href = item.link;
        }
      }else if (item.is_h5 == 1 && item.link) {
        window.location.href = item.link;
      }
    },
    initWxhtml() {
      if (this.fun.getPhoneEnv() == 3) {
        this.clientWidth = 375;
      } else {
        this.clientWidth = document.body.clientWidth;
      }
      let script = document.createElement("script");
      script.type = "text/wxtag-template";
      script.text = `<div style="width: ${this.clientWidth}px;height: ${ this.datas.text_size + 4 }px;"></div>`;
      this.wxhtml = `<wx-open-launch-weapp username="${this.datas.link.gh_id}" path="${this.datas.link.minApp_link}">${script.outerHTML}</wx-open-launch-weapp>`;
    }
  }
};
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  .component-wrapper {
    display: block;
    max-width: 100%;
    text-align: center;
    margin-left: auto;
    margin-right: auto;
    overflow: hidden;
    text-decoration: none;
    position: relative;
  }
</style>
